package com.base.dynamicprograming;

/**
 * 931. 下降路径最小和
 *
 * @author leon
 * @date 2021年12月13日 12:13
 */
public class MinFallingPathSum {

    public int minFallingPathSum(int[][] matrix) {
        int n = matrix.length;
        int[] dp = new int[n];
        int res = 10000;
        for(int i = 0;i<n;i++){
            int[] cur = new int[n];
            for(int j =0;j<n;j++){

                if(i == 0){
                    cur[j] = matrix[i][j];
                }else {
                    // 10000是这道题的极限值
                    cur[j] = Math.min(dp[j],Math.min(j>0?dp[j-1]:10000,j<n-1?dp[j+1]:10000))+matrix[i][j];
                }

                if(i == n-1){
                    res = Math.min(cur[j],res);
                }
            }
            dp = cur;
        }
        return res;
    }

    public static void main(String[] args) {
        int[][] mat = new int[100][100];
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {

                mat[i][j] =100;
            }
        }
        System.out.println(new MinFallingPathSum().minFallingPathSum(mat));
    }
}
